package com.df.tv.launcher.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkStatus {

    private static NetworkStatus INSTANCE;

    private NetworkStatus() {}

    public static NetworkStatus getInstance() {
        if (INSTANCE == null)
            INSTANCE = new NetworkStatus();

        return INSTANCE;
    }

    //获取ip地址
    public String getIpAddress(String name) {
        try {
            NetworkInterface ni = null;
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                ni = en.nextElement();
                if (name.equals(ni.getName()))
                    break;
                ni = null;
            }
            if (ni != null) {
                InetAddress ia = null;
                for (Enumeration<InetAddress> en = ni.getInetAddresses(); en.hasMoreElements();) {
                    ia = en.nextElement();
                    if (ia instanceof Inet4Address)
                        return ia.getHostAddress();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 判断是否wifi
    public boolean isWiFiActive(Context inContext) {
        Context context = inContext.getApplicationContext();
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getTypeName().equals("WIFI") && info[i].isConnected()) { return true; }
                }
            }
        }
        return false;
    }
}
